package com.panasonic.web.controller.cms;

import com.panasonic.common.annotation.Log;
import com.panasonic.common.config.PanasonicConfig;
import com.panasonic.common.core.controller.BaseController;
import com.panasonic.common.core.domain.AjaxResult;
import com.panasonic.common.core.page.TableDataInfo;
import com.panasonic.common.enums.BusinessType;
import com.panasonic.common.utils.DateUtils;
import com.panasonic.common.utils.file.FileUploadUtils;
import com.panasonic.web.config.ServerConfig;
import com.panasonic.web.domain.cms.CmsAttachment;
import com.panasonic.web.service.ICmsAttachmentService;
import com.panasonic.web.util.CmsSecurityUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 附件Controller
 *
 * @author lwf
 * @date 2021-11-12
 */
@RestController
@RequestMapping("/cms/attachment")
@Api(value = "CmsAttachment", tags = "附件")
public class CmsAttachmentController extends BaseController {
    @Autowired
    private ICmsAttachmentService cmsAttachmentService;
    @Autowired
    private ServerConfig serverConfig;

    @PostMapping("/upload")
    @ResponseBody
    public AjaxResult uploadFile(MultipartFile file, CmsAttachment attachment) throws Exception {
        try {

            // 上传文件路径
            String filePath = PanasonicConfig.getUploadPath();
            // 上传并返回新文件名称
            String fileName = FileUploadUtils.upload(filePath, file);
            String url = serverConfig.getUrl() + fileName;
            AjaxResult ajax = AjaxResult.success();
            ajax.put("fileName", fileName);
            ajax.put("url", url);

            attachment.setFileName(fileName);
            attachment.setFileUrl(url);
            attachment.setSize(getFileSizeString(file.getSize()));

            attachment.setCreateBy(CmsSecurityUtils.getUsername());
            attachment.setCreateTime(DateUtils.getNowDate());
            String ext = FileUploadUtils.getExtension(file);
            attachment.setFileType(ext);
            cmsAttachmentService.insertCmsAttachment(attachment);
            ajax.put("guid", attachment.getId());
            return ajax;
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }

    private String getFileSizeString(long size) {

        double mbSize = size / 1024d / 1024d;
        return String.format("%.2f", mbSize)+" MB ";
    }

    /**
     * 查询附件列表
     */
    @ApiOperation(value = "查询")
    @GetMapping("/list")
    public TableDataInfo list(CmsAttachment cmsAttachment) {
        startPage();
        List<CmsAttachment> list = cmsAttachmentService.selectCmsAttachmentList(cmsAttachment);
        return getDataTable(list);
    }



    /**
     * 获取附件详细信息
     */
    @ApiOperation(value = "获取附件详细信息")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(cmsAttachmentService.selectCmsAttachmentById(id));
    }

    /**
     * 新增附件
     */
    @ApiOperation(value = "新增附件")
    @Log(title = "附件", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CmsAttachment cmsAttachment) {
        return toAjax(cmsAttachmentService.insertCmsAttachment(cmsAttachment));
    }

    /**
     * 修改附件
     */
    @ApiOperation(value = "修改附件")
    @Log(title = "附件", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CmsAttachment cmsAttachment) {
        return toAjax(cmsAttachmentService.updateCmsAttachment(cmsAttachment));
    }

    /**
     * 删除附件
     */
    @ApiOperation(value = "删除附件")
    @Log(title = "附件", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(cmsAttachmentService.deleteCmsAttachmentByIds(ids));
    }


}
